/**
 * 
 */
package com.cnhaimei.vod.net.dq;

import android.R.integer;

import com.cnhaimei.vod.net.XVod;

/**
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-8-28
 * 
 */
public class DQQuery extends DQRequest {
	public final static String TABLE_SONG = "song_valid";//歌曲
	public final static String TABLE_STAR = "star_valid";//歌星
	public final static String TABLE_FILM = "star_valid";//电影
	public final static String TABLE_ZJ	  = "song_valid";//专辑
	public final static String TABLE_HOTRANK_SONG = "song_vyear";//歌曲排行
	public final static String TABLE_HOTRANK_STAR = "star_vyear";//歌星排行
	
	public final static String QRY_SONG_ALL = "STYLE != \"电\"";//"STYLE != \"劲\" && STYLE != \"慢\" && STYLE != \"新\" && STYLE != \"广\" && STYLE != \"企\"";// NAME_LN >= 0";
	public final static String QRY_SONG_LIST = "STYLE != \"电\" && NAME_LN >= 3";
	public final static String QRY_STAR_ALL = "NAME_LN >= 0";
	
	public final static String QRY_STYLE_COMMON = "STYLE != \"电\"";
	public final static String QRY_STYLE_FILM = "STYLE == \"电\"";

	public final static String QRY_STAR_GT_NAN = "STATE @> \"港台\" && CLASS == \"0\"";
	public final static String QRY_STAR_GT_NV = "STATE @> \"港台\" && CLASS == \"1\"";
	public final static String QRY_STAR_DL_NAN = "STATE @> \"大陆\" && CLASS == \"0\"";
	public final static String QRY_STAR_DL_NV = "STATE @> \"大陆\" && CLASS == \"1\"";
	public final static String QRY_STAR_GW_NAN = "STATE @> \"国外\" && CLASS == \"0\"";
	public final static String QRY_STAR_GW_NV = "STATE @> \"国外\" && CLASS == \"1\"";
	public final static String QRY_STAR_ZH = "STATE @> \"组合\" && CLASS == \"2\"";

	public final static String QRY_BARSONG_HipHop = "STYLE @> \"Bs\"";//开场音乐
	public final static String QRY_BARSONG_House = "STYLE @> \"Bf\"";//沙发音乐
	public final static String QRY_BARSONG_RB = "STYLE @> \"Bk\"";//麻辣劲k
	public final static String QRY_BARSONG_Club = "STYLE @> \"Bn\"";//夜店音乐
	public final static String QRY_BARSONG_ManYao = "STYLE @> \"Bd\"";//DJ舞曲
	public final static String QRY_BARSONG_JinWu = "STYLE @> \"Bt\"";//派对音乐
	public final static String QRY_BARSONG_DJ = "STYLE @> \"Bd\"";
	public final static String QRY_BARSONG_3D = "STYLE @> \"3D\"";

	public final static String QRY_FILM_Dongzuo = "MODE @> \"1\"";
	public final static String QRY_FILM_Wuxia = "MODE @> \"2\"";
	public final static String QRY_FILM_Xiju = "MODE @> \"3\"";
	public final static String QRY_FILM_Aiqing = "MODE @> \"4\"";
	public final static String QRY_FILM_Kehuan = "MODE @> \"5\"";
	public final static String QRY_FILM_Mohuan = "MODE @> \"6\"";
	public final static String QRY_FILM_Kongbu = "MODE @> \"7\"";
	public final static String QRY_FILM_Zainan = "MODE @> \"8\"";
	public final static String QRY_FILM_Zhanzheng = "MODE @> \"9\"";
	public final static String QRY_FILM_Zuian = "MODE @> \"10\"";
	public final static String QRY_FILM_Lunli = "MODE @> \"11\"";
	public final static String QRY_FILM_Donghua = "MODE @> \"12\"";
	public final static String QRY_FILM_Wenyi = "MODE @> \"13\"";
	public final static String QRY_FILM_3D = "MODE @> \"14\"";
	

	//电影专辑
	public final static String getFilmZj(String zjName){
		return "STAR @> \"|" + zjName + "|\"";
	}
	//歌星专辑
	public final static String getStarZj(String zjName){
		return "STAR @> \"|" + zjName + "|\"";
	}
	
	private Object lock = new Object();

	protected String table;
	protected String query;
	
	protected int snap;
	
	protected boolean autoClose;

	public DQQuery() {
		super();
	}

	public DQQuery(String tab, String que) {
		super();
		this.table = tab;
		this.query = que;
	}
	
	public void setToClose() {
		synchronized (lock) {
			autoClose = true;
		}
	}
	
	public boolean isSetToClose() {
		return autoClose;
	}
	
	public void setSnap(int snap) {
		synchronized (lock) {
			this.snap = snap;
		}
	}
	
	public int getSnap() {
		synchronized (lock) {
			return this.snap;
		}
	}

	@Override
	public void onResponsed() {
		synchronized (lock) {
			if (autoClose && snap != 0) {
				XVod.G().addRequest(new DQClose(snap)); // 自动关闭
			}
		}
	}

	void doRequest(DQManager manger) {
		manger.requestQuery(table, query);
	}

	/**
	 * @return the table
	 */
	public String getTable() {
		return table;
	}

	/**
	 * @param table
	 *            the table to set
	 */
	public void setTable(String table) {
		this.table = table;
	}

	/**
	 * @return the query
	 */
	public String getQuery() {
		return query;
	}

	/**
	 * @param query
	 *            the query to set
	 */
	public void setQuery(String query) {
		this.query = query;
	}
	
	public static String createLikeName(String name) {
//		name = name.toUpperCase();
//		return "NAME_PY @> \"" + name + "\"";
		return "NAME @> \"" + name + "\" && NAME_LN >= 0";
	}
	
	public static String createEquName(String name) {
//		name = name.toUpperCase();
//		return "NAME_PY @> \"" + name + "\"";
		return "NAME == \"" + name + "\" && NAME_LN >= 0";
	}
	
//	public static String createStarLikeName(String name) {
////		name = name.toUpperCase();
////		return "NAME_PY @> \"" + name + "\"";
//		return "NAME @> \"|" + name + "\"| && NAME_LN >= 0";
//	}
	
	public static String createSongLikeName(String name, String queryString) {
		String up = name.toUpperCase();
		if (queryString == null || queryString.length() == 0) {
			queryString = QRY_SONG_ALL;
		}
		return "( ( NAME @> \"" + name + "\" )  || (NAME_PY @* \"" + up + "\" ) ) && NAME_LN >= 0 && " + queryString;
	}
	
	public static String createStarLikeName(String name) {
		String up = name.toUpperCase();
		return "( ( NAME @> \"" + name + "\" )  || (NAME_PY @* \"" + up + "\" ) ) && NAME_LN >= 0";
	}
	
	public static String getStarByLength(int len) {
		return "NAME_LN == " + len;
	}
	
	public static String getSongsEquStarName(String star) {
		return "STAR == \"|" + star + "|\"";//"STAR == \"|" + star + "|\"  && " + QRY_SONG_ALL;
	}

	public static String getSongsContainStarName(String star) {
		return "STAR @> \"|" + star + "|\"";//"STAR == \"|" + star + "|\"  && " + QRY_SONG_ALL;
	}

	//获取某个歌星指定的歌
	public static String getSpecifiedSongsEquStarName(String star, String songName) {
		String up = songName.toUpperCase();
		return "( ( NAME @> \"" + songName + "\" )  || (NAME_PY @* \"" + up + "\" ) ) && NAME_LN >= 0 && STAR @> \"|" + star + "|\"";//"STAR == \"|" + star + "|\"  && " + QRY_SONG_ALL;
	}
	
	//根据长度获取歌曲
	public static String getSongsByLength(int len, String queryString) {
		if (queryString == null || queryString.length() == 0) {
			queryString = QRY_SONG_ALL;
		}
		return String.format("NAME_LN == %d && " + queryString, len);
	}
	
	@Override
	public String description() {
		return "dqquery table:" + table + "query:" + query ;
	}
	
}
